
#include <fstream>
#include "gflags/gflags.h"
#include "glog/logging.h"
#include "gps.pb.h"
#include "gps_proto_util.h"
#include "gpx_parser.h"

DEFINE_string(ingpx, "", "file.gpx");
DEFINE_string(outgpb, "", "file.gpb");

int main(int argc, char** argv) {
  google::InitGoogleLogging(argv[0]);
  google::ParseCommandLineFlags(&argc, &argv, false);
  if (FLAGS_ingpx.empty()) {
    LOG(ERROR) << "missing arg --ingpx file.gpx";
    return 1;
  }
  if (FLAGS_outgpb.empty()) {
    LOG(ERROR) << "missing arg --outgpb file.gpb";
    return 1;
  }
  gps_proto::GpsProto gps_proto;
  if (!gps_proto::GpxParser::ParseFromFile(FLAGS_ingpx.c_str(), &gps_proto)) {
    LOG(ERROR) << "parse failed: " << FLAGS_ingpx;
    return 1;
  }

  std::ofstream output_gpb(FLAGS_outgpb.c_str(),
                           std::ios_base::out|std::ios_base::binary);
  if (!gps_proto.SerializeToOstream(&output_gpb)) {
    LOG(ERROR) << "write failed: " << FLAGS_outgpb;
    return 1;
  }

  gps_proto::BoundsProto bounds_proto;
  LOG(INFO) << "num <trkpts>: " << gps_proto::ComputeBounds(gps_proto,
                                                            &bounds_proto);
  LOG(INFO) << "max lat " << bounds_proto.maxlat();
  LOG(INFO) << "min lat " << bounds_proto.minlat();
  LOG(INFO) << "max lon " << bounds_proto.maxlon();
  LOG(INFO) << "min lon " << bounds_proto.minlon();
  return 0;
}

